//
//  LXActionSheet.m
//  LXActionSheet
//
//  Created by lachesis-zhu on 16/4/26.
//  Copyright © 2016年 yafeng.zhu@lachesis-mh.com. All rights reserved.
//

#import "LXActionSheet.h"
#import "LXActionCell.h"

@interface LXActionSheet ()<UITableViewDataSource , UITableViewDelegate , UIGestureRecognizerDelegate>

@property (nonatomic , strong) UITableView *tableview;
@property (nonatomic , strong) NSArray *listData;
@property (nonatomic , strong) NSString *title;
@property (nonatomic , strong) UIView *customerView;

@end

@implementation LXActionSheet
/**
 *  <#Description#>
 *
 *  @param title             <#title description#>
 *  @param delegate          <#delegate description#>
 *  @param cancelButtonTitle <#cancelButtonTitle description#>
 *  @param titlesArray       <#titlesArray description#>
 *
 *  @return <#return value description#>
 */
- (id)initWithTitle:(NSString *)title delegate:(id<LXActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:(NSArray *)titlesArray{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        self.backgroundColor = RGBACOLOR(160, 160, 160, 0);
        _delegate = delegate;
        if ([LXAutoLength lx_L:60] * [titlesArray count] > (ScreenHeight - [LXAutoLength lx_L:80])) {
            _tableview = [[UITableView alloc] initWithFrame:CGRectMake([LXAutoLength lx_L:10], 0, ScreenWidth - [LXAutoLength lx_L:20], ScreenHeight - [LXAutoLength lx_L:80]) style:UITableViewStylePlain];
            _tableview.scrollEnabled = YES;
        } else {
            _tableview = [[UITableView alloc] initWithFrame:CGRectMake([LXAutoLength lx_L:10], 0, ScreenWidth - [LXAutoLength lx_L:20], [LXAutoLength lx_L:60] * [titlesArray count]) style:UITableViewStylePlain];
            _tableview.scrollEnabled = NO;
        }
        _tableview.dataSource = self;
        _tableview.delegate = self;
        _tableview.layer.cornerRadius = 5;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        
        UILabel *cancelLabel = [[UILabel alloc] initWithFrame:CGRectMake([LXAutoLength lx_L:10], CGRectGetHeight(_tableview.frame) + [LXAutoLength lx_L:10], ScreenWidth - [LXAutoLength lx_L:20], [LXAutoLength lx_L:60])];
        cancelLabel.layer.cornerRadius = 5;
        cancelLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;
        
        cancelLabel.text = @"Cancel";
        cancelLabel.font = [UIFont lx_systemFontOfSize:20];
        cancelLabel.textAlignment = NSTextAlignmentCenter;
        cancelLabel.textColor = kAppNormalTextColor;
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
        cancelLabel.userInteractionEnabled = YES;
        [cancelLabel addGestureRecognizer:tapRecognizer];
        
        _customerView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, CGRectGetHeight(_tableview.frame) + [LXAutoLength lx_L:80])];
        _customerView.backgroundColor = [UIColor clearColor];
        [_customerView addSubview:_tableview];
        [_customerView addSubview:cancelLabel];
        
        [self addSubview:_customerView];
        
        _listData = titlesArray;
        _title = title;
        
    }
    return self;
}

//如果tableview处于uiview上面，uiview整个背景有点击事件，但是我们需要如果我们点击tableview的时候，处理tableview的点击事件，而不是uiview的事件。在这里，我们需要判断我们点击事件是否在uiview上还是在uitableview上。
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if([touch.view isKindOfClass:[self class]]){
        return YES;
    }
    return NO;
}
/**
 *  <#Description#>
 */
-(void)animeData{
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
    tapGesture.delegate = self;
    [self addGestureRecognizer:tapGesture];
    self.userInteractionEnabled = YES;
    
    [UIView animateWithDuration:0.25f animations:^{
        self.backgroundColor = RGBACOLOR(160, 160, 160, 0.4);
        CGRect originRect = _customerView.frame;
        originRect.origin.y = ScreenHeight - CGRectGetHeight(_customerView.frame);
        _customerView.frame = originRect;
    } completion:^(BOOL finished) {
        
    }];
}
/**
 *  <#Description#>
 */
-(void) tappedCancel{
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 0;
        CGRect originRect = _customerView.frame;
        originRect.origin.y = ScreenHeight;
        _customerView.frame = originRect;
    } completion:^(BOOL finished) {
        if (finished) {
            for (UIView *v in _customerView.subviews) {
                [v removeFromSuperview];
            }
            [_customerView removeFromSuperview];
        }
    }];
}
/**
 *  <#Description#>
 *
 *  @param view <#view description#>
 */
- (void) showInView:(UIView *)view{
    if (view) {
        [view.window addSubview:self];
    } else {
        [[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:self];
    }
    [self animeData];
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_title) {
        return [_listData count] + 1;
    } else {
        return [_listData count];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [LXAutoLength lx_L:60];
}
/**
 *
 *
 *  @param tableView <#tableView description#>
 *  @param indexPath <#indexPath description#>
 *
 *  @return <#return value description#>
 */
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_title) {
        if (indexPath.row == 0) {
            static NSString *cellIdentifier = @"TitleCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (cell==nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            }
            cell.textLabel.text = _title;
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            cell.userInteractionEnabled = NO;
            return cell;
        } else {
            static NSString *cellIdentifier = @"DownSheetCell";
            LXActionCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (cell==nil) {
                cell = [[LXActionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            }
            [cell setData:[_listData objectAtIndex:indexPath.row - 1]];
            return cell;
        }
    } else {
        static NSString *cellIdentifier = @"DownSheetCell";
        LXActionCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell==nil) {
            cell = [[LXActionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        [cell setData:[_listData objectAtIndex:indexPath.row]];
        return cell;
    }
}
/**
 *  <#Description#>
 *
 *  @param tableView <#tableView description#>
 *  @param indexPath <#indexPath description#>
 */
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self tappedCancel];
    if (_delegate != nil && [_delegate respondsToSelector:@selector(LXActionSheet:didSelectIndex:)]) {
        [_delegate LXActionSheet:self didSelectIndex:indexPath.row];
        return;
    }
}

@end
